<?php

/**
 * 分类列表
 */

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');

if (isset($_REQUEST['id']))
{
    $cat_id = intval($_REQUEST['id']);
}
else
{
    errorMsg('没有该分类');	  //添加到language包中
}

$page = isset($_REQUEST['page'])   && intval($_REQUEST['page'])  > 0 ? intval($_REQUEST['page'])  : 1;
//$pagesize = 10;     
$pagesize = 100;     //要求一次性返回全部
$sort  = (isset($_REQUEST['sort'])  && in_array(trim($_REQUEST['sort']), array('priceUp', 'priceDown', 'popularUp', 'saleNum'))) ? trim($_REQUEST['sort'])  : "default";
/* 统一数据库字段格式 'buy_sum', 'sort_orders', 'click_count', 'shop_price'*/
$order = 'DESC';
switch ($sort) {
	case 'priceUp':
		$sort = 'shop_price';
	    $order = 'ASC';
		break;
	case 'priceDown':
		$sort = 'shop_price';
		break;
	case 'popularUp':
		$sort = 'click_count';
		break;
	case 'saleNum':
		$sort = 'buy_sum';	
			break;
	default:
		$sort = 'sort_orders';
		break;
}

$children = get_children($cat_id);   //出处：lib_common.php

$count = get_cagtegory_goods_count($children);    //该分类下的所有商品

// if($page*$pagesize>$count)  //请求的页号超过总共的页数   //唉，取消分页
// {
//     errorMsg('没有此页');
// }

$goodslist = category_get_goods($children, $pagesize, $page, $sort, $order);

$response = array('response'=>'category', 'goodslist'=>$goodslist, 'listCount'=>$count);

echo tourldecode($response);


/**
 * 获得分类下的商品总数
 *
 * @access  public
 * @param   string     $cat_id
 * @return  integer
 */
function get_cagtegory_goods_count($children, $ext='')
{
    $where  = "g.is_on_sale = 1 AND g.is_alone_sale = 1 AND g.is_delete = 0 AND ($children OR " . get_extension_goods($children) . ')';
    /* 返回商品总数 */
    return $GLOBALS['db']->getOne('SELECT COUNT(*) FROM ' . $GLOBALS['ecs']->table('goods') . " AS g WHERE $where $ext");
}

/**
 * 获得分类下的商品
 *
 * @access  public
 * @param   string  $children
 * @return  array
 */
function category_get_goods($children, $pagesize, $page, $sort, $order)
{
    
    $where = "g.is_on_sale = 1 AND g.is_alone_sale = 1 AND ".
            "g.is_delete = 0 AND ($children OR " . get_extension_goods($children) . ')';

    /* 获得商品列表 */
    $sql = 'SELECT g.goods_id, g.goods_name, g.click_count, g.shop_price, g.market_price, g.promote_price, g.promote_start_date, g.promote_end_date, g.goods_thumb , g.goods_img, g.goods_type,' .
                "(select sum(goods_number) from " . $GLOBALS['ecs']->table('order_goods') . " WHERE goods_id = g.goods_id ) AS buy_sum ".
                'FROM ' . $GLOBALS['ecs']->table('goods') . ' AS g ' . 
                "WHERE $where ORDER BY $sort $order";

    $res = $GLOBALS['db']->selectLimit($sql, $pagesize, ($page - 1) * $pagesize);

    $arr = array();
    while ($row = $GLOBALS['db']->fetchRow($res))
    {
        if ($row['promote_price'] > 0)
        {
            $promote_price = bargain_price($row['promote_price'], $row['promote_start_date'], $row['promote_end_date']);
        }
        else
        {
            $promote_price = 0;
        }


        $arr[$row['goods_id']]['id']         = $row['goods_id'];
        $arr[$row['goods_id']]['name']       = urlencode($row['goods_name']);
        $arr[$row['goods_id']]['clickCount']      = $row['click_count'];
        $arr[$row['goods_id']]['marketPrice']     = $row['market_price']!=0 ? $row['market_price'] : (string)($row["shop_price"]*1.2);
        $arr[$row['goods_id']]['shopPrice']       = $row['shop_price'];
        //$arr[$row['goods_id']]['type']             = $row['goods_type'];
        $arr[$row['goods_id']]['promotePrice']    = (string)$promote_price;
        $arr[$row['goods_id']]['picture']      = urldecode($_SERVER['HTTP_HOST'] . '/' . $row['goods_thumb']);   //小图
        //$arr[$row['goods_id']]['goods_img']        = $row['goods_img'];    //中图
        //$arr[$row['goods_id']]['url']              = build_uri('goods', array('gid'=>$row['goods_id']), $row['goods_name']);
        $arr[$row['goods_id']]['buySum']          = $row['buy_sum'];
        //$arr[$row['goods_id']]['thumb_list']       = get_thumb($row['goods_id']);
        
        $return[] = $arr[$row['goods_id']];          //去掉数组索引
    }
    
    return $return;
}


?>